define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {

            // 初始化表格参数配置
            Table.api.init({
                showFooter:true,
                extend: {
                    index_url: 'statistics/yard/index' + location.search,
                    add_url: 'statistics/yard/add',
                    edit_url: 'statistics/yard/edit',
                    del_url: 'statistics/yard/del',
                    multi_url: 'statistics/yard/multi',
                    import_url: 'statistics/yard/import',
                    table: 'statistics_yard',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormVisible: true,
                columns: [
                    [

                        {field: 'organ_city', title: __('Organ_city'), operate: 'LIKE'},
                        {field: 'organ_name', title: __('Organ_name'), operate: 'LIKE'},
                        {field: 'can_use', title: '可用数量', operate: false,footerFormatter: function (data) {
                                var field = this.field;
                                var total_sum = data.reduce(function (sum, row) {
                                    return (sum) + (parseFloat(row[field]) || 0);
                                }, 0);
                                return total_sum;
                            }
                             },
                        {field: 'pre_allot', title: '预调入数量', operate: false,footerFormatter: function (data) {
                                var field = this.field;
                                var total_sum = data.reduce(function (sum, row) {
                                    return (sum) + (parseFloat(row[field]) || 0);
                                }, 0);
                                return total_sum;
                            }},
                        {field: 'pre_restore', title: '预还入数量', operate: false,footerFormatter: function (data) {
                                var field = this.field;
                                var total_sum = data.reduce(function (sum, row) {
                                    return (sum) + (parseFloat(row[field]) || 0);
                                }, 0);
                                return total_sum;
                            }},
                        {field: 'bin_number', title: '总箱数', operate: false,footerFormatter: function (data) {
                                var field = this.field;
                                var total_sum = data.reduce(function (sum, row) {
                                    return (sum) + (parseFloat(row[field]) || 0);
                                }, 0);
                                return total_sum;
                            }},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
